package com.yuedao.sender.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yuedao.sender.model.OrderCancel;
import com.yuedao.sender.model.OrderCreate;
import com.yuedao.sender.model.OrderMatch;
import com.yuedao.sender.service.OrderCancelRepository;
import com.yuedao.sender.service.OrderCreateRepository;
import com.yuedao.sender.service.OrderMatchRepository;
import com.yuedao.sender.service1.OrderService;
import com.yuedao.sender.util.AppLogger;


@RestController
@RequestMapping("/order")
public class OrderController {

	@Autowired
	OrderMatchRepository orderMatchRepository;
	
	@Autowired
	OrderCancelRepository orderCancelRepository;
	
	@Autowired
	OrderCreateRepository orderCreateRepository;
	
	@Autowired
	OrderService orders;
	
	protected static AppLogger logger = AppLogger.getInstance();
	
	@RequestMapping("/orderCancel")
    public String orderCancel() {
		List<OrderCancel> list = new ArrayList<OrderCancel>();
		String message = null;
		try {
			list = orderCancelRepository.findBySendFlag(0);
			
			message=orders.sendOrderCancel(list);
			
			//将发送过的数据标志字段(send_flag)更新为1
			for(int i=0;i < list.size();i++){
				list.get(i).setSendFlag(1);
			}
			orderCancelRepository.save(list);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return message;
	}

    @RequestMapping("/orderCreate")
    public String orderCreate() {
    	List<OrderCreate> list = new ArrayList<OrderCreate>();
		String message = null;
		try {
			list = orderCreateRepository.findBySendFlag(0);

			message=orders.sendOrderCreate(list);
			//将发送过的数据标志字段(send_flag)更新为1
			for(int i=0;i < list.size();i++){
				list.get(i).setSendFlag(1);
			}
			orderCreateRepository.save(list);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return message;
	
    }

    @RequestMapping("/orderMatch")
    public String orderMatch() {
    	List<OrderMatch> list = new ArrayList<OrderMatch>();
		String message = null;
		try {
			list = orderMatchRepository.findBySendFlag(0);

			message=orders.sendOrderMatch(list);
			//将发送过的数据标志字段(send_flag)更新为1
			for(int i=0;i < list.size();i++){
				list.get(i).setSendFlag(1);
			}
			orderMatchRepository.save(list);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return message;
	
    }
	
}
